﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 顶点
    /// </summary>
    /// <remarks>对应 SketchUpAPI 中的 SUVertexRef </remarks>
    public class Vertex : Entity
    {
        #region API 函数
        /// <summary>
        /// 获取坐标点
        /// </summary>
        /// <param name="vertex"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUVertexGetPosition(IntPtr vertex, ref Point3D position);

        /// <summary>
        /// 设置顶点坐标
        /// </summary>
        /// <param name="vertex"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUVertexSetPosition(IntPtr vertex, Point3D position);

        /// <summary>
        /// 获取顶点链接的边缘个数
        /// </summary>
        /// <param name="vertex"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUVertexGetNumEdges(IntPtr vertex, out long count);

        /// <summary>
        /// 获取顶点链接的边缘
        /// </summary>
        /// <param name="vertex"></param>
        /// <param name="len"></param>
        /// <param name="edges"></param>
        /// <param name="realcount"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUVertexGetEdges(IntPtr vertex, long len, IntPtr[] edges, out long realcount);

        /// <summary>
        /// 顶点链接的面的个数
        /// </summary>
        /// <param name="vertex"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUVertexGetNumFaces(IntPtr vertex, out long count);

        /// <summary>
        /// 顶点链接的面
        /// </summary>
        /// <param name="vertex"></param>
        /// <param name="len"></param>
        /// <param name="faces"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUVertexGetFaces(IntPtr vertex, long len, IntPtr[] faces, out long count);

        /// <summary>
        /// 顶点链接的环的个数
        /// </summary>
        /// <param name="vertex"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUVertexGetNumLoops(IntPtr vertex, out long count);

        /// <summary>
        /// 顶点链接的环
        /// </summary>
        /// <param name="vertex"></param>
        /// <param name="len"></param>
        /// <param name="loops"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUVertexGetLoops(IntPtr vertex, long len, IntPtr[] loops, out long count);
        #endregion

        /// <summary>
        /// 坐标点
        /// </summary>
        public Point3D Position
        {
            get
            {
                Point3D pt = Point3D.Origin;
                SketchUpExceptionHelper.ThrowSUResult(SUVertexGetPosition(Id, ref pt));
                return pt;
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUVertexSetPosition(Id, value));
            }
        }

        /// <summary>
        /// 顶点链接的边缘
        /// </summary>
        public List<Edge> Edges
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUVertexGetNumEdges(Id, out long count));
                IntPtr[] ptrs = new IntPtr[count];
                SketchUpExceptionHelper.ThrowSUResult(SUVertexGetEdges(Id, count, ptrs, out long realcount));
                return ptrs.Select(pt => new Edge(pt)).ToList();
            }
        }

        /// <summary>
        /// 顶点链接的全部环
        /// </summary>
        public List<Loop> Loops
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUVertexGetNumLoops(Id, out long count));
                IntPtr[] ptrs = new IntPtr[count];
                SketchUpExceptionHelper.ThrowSUResult(SUVertexGetLoops(Id, count, ptrs, out long realcount));
                return ptrs.Select(pt => new Loop(pt)).ToList();
            }
        }

        /// <summary>
        /// 顶点链接的全部面
        /// </summary>
        public List<Face> Faces
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUVertexGetNumFaces(Id, out long count));
                IntPtr[] ptrs = new IntPtr[count];
                SketchUpExceptionHelper.ThrowSUResult(SUVertexGetFaces(Id, count, ptrs, out long realcount));
                return ptrs.Select(pt => new Face(pt)).ToList();
            }
        }

        internal Vertex(IntPtr id)
        {
            Id = id;
        }
    }
}